package com.example.lanouhn.bantangapp.activities.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.activities.Home_Sign_Activity;
import com.example.lanouhn.bantangapp.activities.activities.MainSeachActivity;
import com.example.lanouhn.bantangapp.activities.adapters.VpSelectedAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/7/22.
 */
public class Fragment_main extends Fragment {

    private ViewPager vp ;
    private TabLayout tab;
    private List<String> tabNames;
    //viewpager的数据集合
    private List<Fragment> fragments;
    //viewPager的适配器
    private VpSelectedAdapter vpfAdapter;
    private LinearLayout  re;//搜索栏

    private ImageView title_image_right;//签到
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main,container,false);
        vp = (ViewPager) view.findViewById(R.id.vp);
        tab= (TabLayout) view.findViewById(R.id.tablayout);
        re= (LinearLayout) view.findViewById(R.id.re);

        vp.setOffscreenPageLimit(9);//设置vp缓存 暂存里面的fragment页面  左右两个

        title_image_right = (ImageView) view.findViewById(R.id.title_image_right);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initData();//添加数据
        setDataToView();//绑定数据到视图
        initLinstener();
    }
//监听点击事件
    private void initLinstener() {
        //搜索栏点击
        re.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(), MainSeachActivity.class);
                startActivity(it);
            }
        });

        //签到 跳转
        title_image_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(), Home_Sign_Activity.class);
                startActivity(it);
            }
        });
    }

    //数据集合
    private void initData() {
        //tab标签名称集合
        tabNames = new ArrayList();
        //Fragment集合
        fragments = new ArrayList();
        //tablayout数据集合
        tabNames.add(getResources().getString(R.string.Selected));
        tabNames.add(getResources().getString(R.string.useroriginal));
        tabNames.add(getResources().getString(R.string.TheHottestWeek));
        tabNames.add(getResources().getString(R.string.Beautywear)+"&"+getResources().getString(R.string.Beautywear1));
        tabNames.add(getResources().getString(R.string.DeliciousFood));
        tabNames.add(getResources().getString(R.string.Gift));
        tabNames.add(getResources().getString(R.string.Designsense));
        tabNames.add(getResources().getString(R.string.literature));
        tabNames.add(getResources().getString(R.string.StudentParty));
        // 添加页面
        fragments.add(new Fragment_Selected());
        fragments.add(new Fragment_Useroriginal());
        fragments.add(new Fragment_ThehottestWeek());
        fragments.add(new Fragment_Beautywear());
        fragments.add(new Fragment_Deliciousfood());
        fragments.add(new Fragment_Gift());
        fragments.add(new Fragment_Designsense());
        fragments.add(new Fragment_literature());
        fragments.add(new Fragment_StudentParty());
    }
    //绑定数据
    private void setDataToView() {
        vpfAdapter = new VpSelectedAdapter(getChildFragmentManager(),tabNames,fragments);
        //设置适配器
        vp.setAdapter(vpfAdapter);
        //tabLayout和ViewPager绑定
        tab.setupWithViewPager(vp);
    }

    }


